package com.xyz.update.update;

import android.app.Dialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;

import com.blankj.utilcode.util.ScreenUtils;
import com.xyz.update.R;
import com.xyz.update.bean.VersionInfo;

public class XyUpdateDialog extends Dialog {

    private VersionInfo info;
    private OnUpdateListener onUpdateListener;
    private int dlgTopResId = -1;
    private int btnColor = -1;

    public interface OnUpdateListener {
        void close(boolean needUpdate);
    }

    public XyUpdateDialog(@NonNull Context context, VersionInfo info, OnUpdateListener listener) {
        super(context, R.style.custom_dialog2);
        this.info = info;
        this.onUpdateListener = listener;
    }

    public XyUpdateDialog(@NonNull Context context, VersionInfo info, int dlgTopResId, int btnColor, OnUpdateListener listener) {
        super(context, R.style.custom_dialog2);
        this.info = info;
        this.dlgTopResId = dlgTopResId;
        this.btnColor = btnColor;
        this.onUpdateListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update_v2);
        if (dlgTopResId == -1) {

            TextView tvCode = findViewById(R.id.tvCode);
            TextView tvContent = findViewById(R.id.tvContent);
            ImageView ivClose = findViewById(R.id.ivClose);
            TextView tvOk = findViewById(R.id.tvOk);

            if (info != null) {
                tvCode.setText(String.format(getContext().getString(R.string.update_code), info.getAppVersion()));
                tvContent.setText(null == info.getDescribe() || "null".equals(info.getDescribe()) || TextUtils.isEmpty(info.getDescribe()) ? "检查到版本更新，请下载新版本！" : info.getDescribe());
                ivClose.setVisibility(info.isCoerceUpdate() ? View.GONE : View.VISIBLE);
                setCanceledOnTouchOutside(!info.isCoerceUpdate());
                setCancelable(!info.isCoerceUpdate());
            }

            tvOk.setOnClickListener(v -> {
                if (onUpdateListener != null) {
                    onUpdateListener.close(true);
                }
                if (ivClose.getVisibility() == View.VISIBLE) {
                    dismiss();
                }
            });
            ivClose.setOnClickListener(v -> {
                if (onUpdateListener != null) {
                    onUpdateListener.close(false);
                }
                dismiss();
            });
        } else {

            AppCompatImageView ivImg = findViewById(R.id.ivImg);
            TextView tvCode = findViewById(R.id.tvCode);
            TextView tvContent = findViewById(R.id.tvContent);
            ImageView ivClose = findViewById(R.id.ivClose);
            TextView tvOk = findViewById(R.id.tvOk);
            if (dlgTopResId > 0) ivImg.setImageResource(dlgTopResId);
            if (btnColor > 0) tvOk.setBackgroundTintList(ColorStateList.valueOf(btnColor));

            if (info != null) {
                tvCode.setText(String.format(getContext().getString(R.string.update_code), info.getAppVersion()));
                tvContent.setText(null == info.getDescribe() || "null".equals(info.getDescribe()) || TextUtils.isEmpty(info.getDescribe()) ? "检查到版本更新，请下载新版本！" : info.getDescribe());
                ivClose.setVisibility(info.isCoerceUpdate() ? View.INVISIBLE : View.VISIBLE);
                setCanceledOnTouchOutside(!info.isCoerceUpdate());
                setCancelable(!info.isCoerceUpdate());
            }

            tvOk.setOnClickListener(v -> {
                if (onUpdateListener != null) {
                    onUpdateListener.close(true);
                }
                if (ivClose.getVisibility() == View.VISIBLE) {
                    dismiss();
                }
            });
            ivClose.setOnClickListener(v -> {
                if (onUpdateListener != null) {
                    onUpdateListener.close(false);
                }
                dismiss();
            });
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        getWindow().setLayout(
                (int) (ScreenUtils.getScreenWidth() * 0.71), ViewGroup.LayoutParams.WRAP_CONTENT
        );
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setWindowAnimations(android.R.style.Animation_Dialog);
    }
}
